# coding=utf-8

"""
@author  zhongy
@date    2018-10-17
@desc    数据子集view
"""

from views.view_base import ViewBase
from common.middlewares import *
from controller.sub_data_set_controller import SubDataSetController
import threading
from conf.settings import settings

class SubDataSetView(ViewBase):
    def __init__(self):
        super(SubDataSetView, self).__init__()

    # 添加数据子集
    @dispatch('/add', methods=['POST'])
    @authenticated()
    @verify_params(sub_set_name='string', data_set_id='int')
    def add(self):
        code = RESPONSE_FAILED
        msg = '添加失败!'

        try:
            sub_set_name = self.params.get('sub_set_name', None)
            data_set_id = self.params.get('data_set_id')
            controller = SubDataSetController()
            sub_data_set = controller.get_by_name(sub_set_name=sub_set_name, data_set_id=data_set_id)
            if sub_data_set is not None:
                code = RESPONSE_FAILED
                msg = sub_set_name + '已存在!'
            else:
                result = controller.add(sub_set_name=sub_set_name, data_set_id=data_set_id)
                if result:
                    code = RESPONSE_SUCCESS
                    msg = '添加成功!'
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data={}, msg=msg)

    # 查询数据子集列表
    @dispatch('/list', methods=['POST'])
    @authenticated()
    def list(self):
        code = RESPONSE_SUCCESS
        msg = '查询成功!'
        data = []

        try:
            sub_set_name = self.params.get('sub_set_name', '')
            data_set_id = self.params.get('data_set_id', None)
            page_index = self.params.get('page_index', 1)
            page_size = self.params.get('page_size', 5)
            controller = SubDataSetController()
            result, pagination = controller.list(sub_set_name=sub_set_name, data_set_id=data_set_id,
                                                 page_index=page_index, page_size=page_size)
            data = {'list': result, 'pagination': pagination}
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data=data, msg=msg)

    # 查询全部数据子集
    @dispatch('/all', methods=['GET', 'POST'])
    @authenticated()
    def all(self):
        code = RESPONSE_SUCCESS
        msg = '查询成功!'
        data = []
        try:
            sub_set_name = self.params.get('sub_set_name', '')
            data_set_id = self.params.get('data_set_id', None)
            controller = SubDataSetController()
            result = controller.all(sub_set_name=sub_set_name, data_set_id=data_set_id)
            if result is not None:
                data = result
        except:
            writelog(traceback.format_exc())
        return self._render.render_json(code=code, data=data, msg=msg)

    # 删除数据子集
    @dispatch('/delete', methods=['POST'])
    @authenticated()
    @verify_params(sub_set_id='int')
    def delete(self):
        code = RESPONSE_FAILED
        msg = '刪除失败!'

        try:
            sub_set_id = self.params.get('sub_set_id')
            controller = SubDataSetController()
            result = controller.update(sub_set_id=sub_set_id, is_deleted=True)
            if result:
                code = RESPONSE_SUCCESS
                msg = '刪除成功!'
        except:
            writelog(traceback.format_exc())

        return self._render.render_json(code=code, data={}, msg=msg)


    @dispatch('/export_shp', methods=['GET', 'POST'])
    @authenticated()
    @verify_params(sub_set_id='int', layer_codes='list', file_name='string')
    def export_shp(self):
        """导出shp文件"""
        try:
            user = self.user
            sub_set_id = self.params.get('sub_set_id')
            layer_codes = self.params.get('layer_codes')
            file_name = self.params.get('file_name')
            path = settings.get('SHAPE_EXPORT_PATH')    # 根目录
            # path = 'D:\\data\\test_data\\'

            if file_name == '':
                return self._render.render_json(code=RESPONSE_FAILED, data={}, msg="不能为空字符串")

            controller = SubDataSetController()

            if controller.check_same_name(path, file_name):   # 查看重名 顺便把目录搞定
                return self._render.render_json(code=RESPONSE_FAILED, data={}, msg="重名")

            t = threading.Thread(target=controller.export_shp,
                                 args=(path, user.user_id, sub_set_id, layer_codes, file_name),
                                 name='线程--- 子集id:' + str(sub_set_id))
            t.start()

            return self._render.render_json(code=RESPONSE_SUCCESS, data={}, msg="导出中")
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data={}, msg="系统异常")

    @dispatch('/get_export_list', methods=['GET', 'POST'])
    @authenticated()
    @verify_params(page_size='int', page_index='int')
    def get_export_list(self):
        """到处任务列表页"""
        try:
            page_size = self.params.get('page_size', 5)
            page_index = self.params.get('page_index', 1)
            controller = SubDataSetController()
            data = controller.get_export_list(page_size, page_index)
            return self._render.render_json(code=RESPONSE_SUCCESS, data=data, msg="查询成功")
        except:
            writelog(traceback.format_exc())
            return self._render.render_json(code=RESPONSE_FAILED, data={}, msg="系统异常")